//
//  DeviceInfoViewController.m
//  Fantasia
//
//  Created by kongkong on 16/10/1.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "DeviceInfoViewController.h"
#import "DeviceInfoModel.h"
#import "DeviceDataCell.h"

@interface DeviceInfoViewController()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) DeviceInfoModel* model;
@end

@implementation DeviceInfoViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    [self setupTitle];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupTitle];
}

- (void)setupTitle{
    NSString* title = [[FantasiaStorage instance]memoryValueForKey:@"currentDevice"];
    self.tabBarController.title =[title stringByAppendingString:@" 设备信息"];

}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.height -= 44;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[DeviceDataCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

- (DeviceInfoModel *)model{
    if (!_model) {
        _model = [DeviceInfoModel api:@"device" action:@"FETCHINFO"];
    }
    return _model;
}

- (void)loadData{
    __weak typeof(self) weakSelf = self;
    [self.model relaodData:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.tableView reloadData];
    }];
}

#pragma -mark tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DeviceDataCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell reloadWithData:[self.model dataModelWithIndex:indexPath]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
@end
